import React from 'react';
import styles from './style.less';
import { Button } from 'antd';
import { Get_Bought_Book } from '@/services/api';
import { history, useLocation } from 'umi';

interface data {
  accomplish_time: string;
  book_author: string;
  book_degree: string;
  book_description: string;
  book_id: string;
  book_image: string;
  book_name: string;
  book_price: number;
  book_publisher: string;
  book_status: string;
  book_type: string;
  buyer_id: string;
  post_time: string;
  seller_id: string;
  trade_time: string;
}

function TimeCard(props: {
  source: data;
  cardType: string;
  getChildrenData(pro: any): any;
}) {
  const location = useLocation();
  const temp = location.pathname.match('/[a-z]+/(.*)');
  console.log('temp', temp);
  const id = temp ? temp[1] : null;

  let base64 = '';
  let showimg = <img className={styles.bookimg} src="" alt="无图片" />;
  if (props.source.book_image != '') {
    base64 = JSON.parse(props.source.book_image).book_image;
    showimg = <img className={styles.bookimg} src={base64} alt="无图片" />;
  }

  function handleGet() {
    try {
      Get_Bought_Book(props.source.book_id).then(() => {
        props.getChildrenData('test');
        history.push(`/mycenter/${id}`);
      });
    } catch (error) {
      console.log(error);
    }
  }

  const type1 = (
    <div className={styles.nobutton}>
      <p>待出售...</p>
    </div>
  );
  const type2 = (
    <div className={styles.nobutton}>
      <p>请尽快发货...</p>
    </div>
  );
  const type3 = <div />;
  const type4 = (
    <Button onClick={handleGet} className={styles.getbook}>
      确认收货
    </Button>
  );
  const type5 = <div />;

  let typeshow;

  switch (props.cardType) {
    case 'my_published':
      typeshow = type1;
      break;
    case 'my_trading':
      typeshow = type2;
      break;
    case 'my_sold':
      typeshow = type3;
      break;
    case 'my_waiting':
      typeshow = type4;
      break;
    case 'my_bought':
      typeshow = type5;
      break;
  }

  let realDay = Number(props.source.post_time.substr(8, 2));
  let realHour = Number(props.source.post_time.substr(11, 2));
  let realTime;
  if (realHour >= 16) {
    realHour = realHour - 16;
    realDay = realDay + 1;
    realTime =
      props.source.post_time.substr(0, 8) +
      realDay.toString() +
      ' 0' +
      realHour.toString() +
      props.source.post_time.substr(13, 6);
  } else {
    realHour = realHour + 8;
    if (realHour < 10)
      realTime =
        props.source.post_time.substr(0, 8) +
        realDay.toString() +
        ' 0' +
        realHour.toString() +
        props.source.post_time.substr(13, 6);
    else {
      realTime =
        props.source.post_time.substr(0, 8) +
        realDay.toString() +
        ' ' +
        realHour.toString() +
        props.source.post_time.substr(13, 6);
    }
  }

  return (
    <div className={styles.main}>
      <div className={styles.picture}>{showimg}</div>
      <div className={styles.details}>
        <div className={styles.rowblock}>
          <p className={styles.bookname}>{props.source.book_name}</p>
          <div className={styles.rowblockprice}>
            <p className={styles.degree}>{props.source.book_degree}</p>
            <p className={styles.price}>￥{props.source.book_price}</p>
          </div>
        </div>
        <div className={styles.rowdescription}>
          <p className={styles.description}>{props.source.book_description}</p>
          <p className={styles.type}>{props.source.book_type}</p>
        </div>
        <div className={styles.rowtime}>
          <p className={styles.time}>{realTime}</p>
          {typeshow}
        </div>
      </div>
    </div>
  );
}

export default TimeCard;
